package jmd.client;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.sql.Date;

import javax.swing.plaf.SliderUI;

public class ClientTest extends Thread
{

	private int port = 1001;
	private static String host = "127.0.0.1";
	private SocketChannel sc = null;
	private ByteBuffer w_buff = ByteBuffer.allocate(1024);
	private Charset charset = Charset.forName("UTF-8");

	public ClientTest()
	{
		InetSocketAddress addr = new InetSocketAddress(host, port);

		try
		{
			sc = SocketChannel.open();

			// 连接到server
			sc.connect(addr);
			while (!sc.finishConnect())
				;
			System.out.println("connection has been established!...");
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	@Override
	public void run()
	{
			try
			{
				Thread.currentThread().sleep(200);
			} catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			w_buff.clear();
			long time = System.currentTimeMillis();
			
			String test = "*ST,2020916012,1,2010/6/2 15:45:11,22.128745,11.3466574,14.28,253,3.50,4293949494,FFFFFBFF#"; 
			System.out.println("test = " +test);
			w_buff = charset.encode(test);
			// w_buff.putInt(10);
			// w_buff.put((byte)1);
			// w_buff.putChar('a');
			// w_buff.putChar('b');
			// w_buff.putChar('c');
			// w_buff.putChar('d');
			// w_buff.flip();
			try
			{
				sc.write(w_buff);
			} catch (IOException e)
			{
				e.printStackTrace();
			}
			System.out.println("w_ff = " + w_buff);
			System.out.println("write successs~~!!");

	}

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		for (int i = 0; i < 1000; i++)
		{
			new ClientTest().start();
		}
		while (true)
		{

		}
	}

}
